package com.snowleopard.common.sql;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.xml.sax.SAXException;

public class SqlList {
	private static Logger logger = Logger.getLogger(SqlList.class);

	protected Map<String, String> sqlMap;

	public SqlList(String sqlFilePath) {
		this.sqlMap = new HashMap<String, String>();
		InputStream is = null;
		try {
			is = super.getClass().getClassLoader().getResourceAsStream(sqlFilePath);
			SqlFileLoader fileLoader = new SqlFileLoader();
			fileLoader.load(is);

			sqlMap.putAll(fileLoader);

			logger.debug("SQL file '" + sqlFilePath + "'was loaded successfuly");
		} catch (IOException e) {
			logger.error("config=" + sqlFilePath + e.getMessage());
		} catch (SAXException se) {
			logger.error(sqlFilePath + " " + se.getMessage(), se);
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					logger.error("config=" + sqlFilePath + e.getMessage());
				}
			}
		}
	}

	public String getSql(String id) {
		return this.sqlMap.get(id);
	}

	public void destroy() {
		this.sqlMap.clear();
	}

}
